<template>
  <div class="commod">
    <!-- 商品 -->
    <div class="container">
      <h3>手机</h3>
      <div class="phones">
        <div class="adv">
          <img v-lazy="'/imgs/mix-alpha.jpg'" alt="" />
        </div>
        <div class="pho-items" @click="toDetail">
          <div
            class="item"
            v-for="item of list"
            :key="item.id"
            :data-id="item.id"
          >
            <span class="title red">新品</span>
            <div class="image">
              <img :src="item.imageHost | setImageUrl(item.mainImage)" />
            </div>
            <span class="name">{{ item.subtitle }}</span>
            <span class="des">{{ item.name }}</span>
            <span class="pres">{{ item.price }}元<span class="iconfont" @click.stop="addCart(item.id)">&#xe600;</span></span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "Commod",
  data() {
    return {
      list: [],
      menuList: [
        [
          {
            id: 30,
            img: "/imgs/item-box-1.png",
            name: "小米CC9",
          },
          {
            id: 31,
            img: "/imgs/item-box-2.png",
            name: "小米8青春版",
          },
          {
            id: 32,
            img: "/imgs/item-box-3.jpg",
            name: "Redmi K20 Pro",
          },
          {
            id: 33,
            img: "/imgs/item-box-4.jpg",
            name: "移动4G专区",
          },
        ],
        [0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 0],
      ],
    };
  },
  methods: {
    toDetail(e) {
      var id = e.target.dataset.id;
      this.$router.push("index/product/" + id);
    },
    addCart(id){
      this.axios.post('/carts',{
        productId: id,
        selected: true
      }).then(() => {
        this.$confirm("前往我的购物车","添加成功",{
          confirmButtonText: "我的购物车",
          cancelButtonText:"不去了"
        }).then(() => {
          this.$router.push('/cart')
        },() => {
          return;
        })
      })
    }
  },
  created() {
    this.axios.get("/products").then((res) => {
      res.list.length = 8;
      this.list = res.list;
      this.$store.commit("saveproducts", res.list);
    });
  },
  mounted() {
    this.list = this.$store.state.productsList;
  },
  filters: {
    setImageUrl: function (baseurl, url) {
      if (url.indexOf("http") === -1) {
        return `${baseurl}/${url}`;
      } else {
        return url;
      }
    },
  },
};
</script>

<style lang="scss">
.commod {
  width: 100%;
  background: #eee;
  overflow: hidden;
  h3 {
    line-height: 82px;
  }
  .phones {
    margin-bottom: 50px;
    display: flex;
    justify-content: space-between;
    .adv {
      margin-right: 16px;
      img {
        width: 224px;
        height: 619px;
      }
    }
    .pho-items {
      height: 619px;
      font-family: FZLanTingHeiS-R-GB;
      .item {
        width: 236px;
        height: 302px;
        display: flex;
        flex-direction: column;
        align-items: center;
        background: #fff;
        float: left;
        margin-right: 10.5px;
        margin-bottom: 14px;
        .red {
          background: red;
        }
        .green {
          background: greenyellow;
        }
        .title {
          font-size: 14px;
          font-weight: bold;
          color: #eee;
          padding: 3px 10px;
          margin-bottom: 40px;
          border-radius: 3px;
        }
        .image {
          max-width: 190px;
          height: 134px;
          img {
            max-width: 190px;
            max-height: 134px;
          }
        }
        .name {
          height: 13px;
          font-size: 14px;
          font-weight: bold;
          color: #333333;
          text-align: center;
          margin-top: 20px;
        }
        .des {
          height: 13px;
          width: 80%;
          font-size: 12px;
          font-weight: bold;
          text-align: center;
          color: #999999;
          margin-top: 5px;
          overflow: hidden;
          text-overflow: ellipsis;
        }
        .pres {
          font-size: 14px;
          font-weight: bold;
          color: #f20a0a;
          margin-top: 13px;
          .iconfont{
            margin-left: 10px;
          }
        }
      }
    }
  }
}
</style>